﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GraphosData;
using System.Windows.Media.Animation;

namespace GraphosControls
{
    /// <summary>
    /// Interaction logic for SearchViewer.xaml
    /// </summary>
    public partial class SearchViewer : UserControl
    {
        private SearchEngine mySearch;

        public SearchViewer()
        {
           
            InitializeComponent();
            DataContext = this;
            MainListBox.SelectionChanged += new SelectionChangedEventHandler(MainListBox_SelectionChanged);
            MainTextBox.KeyDown += new KeyEventHandler(MainTextBox_KeyDown);
        }

        public IList<Node> SearchResults
        {
            get { return (IList<Node>)GetValue(SearchResultsProperty); }
            set { SetValue(SearchResultsProperty, value); }
        }
        public static readonly DependencyProperty SearchResultsProperty =
            DependencyProperty.Register("SearchResults", typeof(IList<Node>), typeof(SearchViewer), new UIPropertyMetadata(new List<Node>()));
        
        public Node SelectedResult
        {
            get { return (Node)GetValue(SelectedResultProperty); }
            set { SetValue(SelectedResultProperty, value); }
        }
        public static readonly DependencyProperty SelectedResultProperty =
            DependencyProperty.Register("SelectedResult", typeof(Node), typeof(SearchViewer), new UIPropertyMetadata(null, new PropertyChangedCallback(OnSelectedResultChanged)));
        private static void OnSelectedResultChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            SearchViewer viewer = sender as SearchViewer;
            if (viewer.MainListBox.Items.Contains(viewer.SelectedResult))
            {
                viewer.MainListBox.SelectedItem = viewer.SelectedResult;
            }
            else
            {
                viewer.MainListBox.SelectedIndex = -1;
            }
        }


        public string CurrentSearchQuery
        {
            get { return (string)GetValue(CurrentSearchQueryProperty); }
            set { SetValue(CurrentSearchQueryProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentSearchQuery.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentSearchQueryProperty =
            DependencyProperty.Register("CurrentSearchQuery", typeof(string), typeof(SearchViewer), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(OnCurrentSearchQueryChanged)));

        private static void OnCurrentSearchQueryChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            SearchViewer viewer = sender as SearchViewer;
            viewer.Search();
            viewer.RaiseEvent(new RoutedEventArgs(ExpanderPanel.MainContentSizeChangedEvent));

            
        }

        public string DidYouMean
        {
            get { return (string)GetValue(DidYouMeanProperty); }
            set { SetValue(DidYouMeanProperty, value); }
        }
        public static readonly DependencyProperty DidYouMeanProperty =
            DependencyProperty.Register("DidYouMean", typeof(string), typeof(SearchViewer), new UIPropertyMetadata(null));
        
        public IList<Node> AllPossibleValues
        {
            get { return (IList<Node>)GetValue(AllPossibleValuesProperty); }
            set { SetValue(AllPossibleValuesProperty, value); }
        }
        public static readonly DependencyProperty AllPossibleValuesProperty =
            DependencyProperty.Register("AllPossibleValues", typeof(IList<Node>), typeof(SearchViewer), new FrameworkPropertyMetadata(
                new List<Node>(), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnAllPossibleValuesChanged)));
        private static void OnAllPossibleValuesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            SearchViewer viewer = sender as SearchViewer;
            viewer.mySearch = new SearchEngine((IList<Node>)args.NewValue);
        }
        /// <summary>
        /// Do a search on the current data in the object using strFind as the search term
        /// </summary>
        /// <param name="strFind">The string to search for</param>
        public void Search()
        {
            SearchResults = mySearch.Search(CurrentSearchQuery);
        }


        void MainTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && MainListBox.Items.Count > 0)
            {
                MainListBox.SelectedIndex = 0;
            }
        }
        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            if (!MainTextBox.IsFocused)
            {
                MainTextBox.Focus();
            }
        }
       
        void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BindingExpression exp = MainListBox.GetBindingExpression(ListBox.SelectedItemProperty);
            exp.UpdateSource();
        }
    }
}
